from typing import Optional

from fastapiApp.models.admin import Menu_Mo
from fastapiApp.schemas.menu_scheams import *
from fastapi.exceptions import HTTPException

class MenuController(Menu_Mo):
    def __init__(self):
        # super().__init__(model=Menu_Mo)
        self.model = Menu_Mo

    # 判断name是否已经存在
    async def is_exist(self,obj_in : MenuCreate) -> int:
        if await self.model.filter(name=obj_in.name).exists():
            raise HTTPException(status_code=400, detail="菜单名字已存在")
        return 0

    # 修改用户信息
    async def update_menu(self, obj_in:MenuUpdate) -> None:
        menu = await self.model.get(id=obj_in.id)
        menu.menu_type = obj_in.menu_type
        menu.name = obj_in.name
        menu.icon = obj_in.icon
        menu.path = obj_in.path
        menu.order = obj_in.order
        menu.parent_id = obj_in.parent_id
        menu.is_hidden = obj_in.is_hidden
        menu.component = obj_in.component
        menu.keepalive = obj_in.keepalive
        menu.redirect = obj_in.redirect
        await menu.save()

    async def get_by_menu_path(self, path: str) -> Optional["Menu_Mo"]:
        return await self.model.filter(path=path).first()


menu_controller = MenuController()
